package com.example.lwt.stopwatch;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private  Watch mWatch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mWatch = (Watch) savedInstanceState.getSerializable("mWatch");
        boolean isRunning = savedInstanceState.getBoolean("isRunning");
        TextView textView = (TextView) findViewById(R.id.time_view);
        mWatch.textView = textView;
        mWatch.update();
        if (isRunning) {
            mWatch.start();
        }
        System.out.println("onRestoreInstanceState");

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart()");
    }

    @Override
    protected void onStop() {
        super.onStop();

        mWatch.stop();
        System.out.println("onStop()");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        boolean isRunning = mWatch.state instanceof WatchStateRun;
        outState.putSerializable("mWatch",mWatch);
        outState.putBoolean("isRunning",isRunning);
        System.out.println("onSaveInstanceState");
    }

    public void onBtnStartClick(View view) {
        if (null == mWatch) {
            TextView textView = (TextView) findViewById(R.id.time_view);
            mWatch = new Watch(textView);
        }
        mWatch.start();
    }

    public void onBtnStopClick(View view) {
        mWatch.stop();
    }

    public void onBtnResetClick(View view) {
        mWatch.reset();

    }


    public void showLinearLayout(View view) {
        Intent showLinearly = new Intent(this, TestLinearLayoutActivity.class);
        startActivity(showLinearly);
    }
}
